package org.iteam.core.service.mysql.impl.sys;

import java.sql.SQLException;
import java.util.List;

import org.iteam.core.model.sys.SysFunction;
import org.iteam.core.service.sys.SysFunctionService;
import org.iteam.core.utils.DateUtil;
import org.iteam.core.utils.SqlBuilder;

public class SysFunctionServiceImpl extends SysFunctionService {
	@Override
	public boolean create(SysFunction obj) throws SQLException {
		SqlBuilder sb = SqlBuilder.createInsert(SysFunction.TABLE_NAME);
		sb.addField("code", obj.getCode());
		sb.addField("name", obj.getName());
		if ("".equals(obj.getParent()))
			obj.setParent(null);
		sb.addField("parent", obj.getParent());
		sb.addField("url", obj.getUrl());
		sb.addField("auth", obj.getAuth());
		sb.addField("type", obj.getType());
		sb.addField("level", obj.getLevel());
		sb.addField("level_code", obj.getLevel_code());
		sb.addField("mode", obj.getMode());
		sb.addField("sort", obj.getSort());
		sb.addField("status", obj.getStatus());
		sb.addField("create_time", DateUtil.currentTime());
		return super.update(sb.getSql().toUpperCase(), sb.getParams());
	}

	@Override
	public List<SysFunction> queryByPage(long page, long rows)
			throws SQLException {
		return super
				.query("select * from "
						+ SysFunction.TABLE_NAME
						+ " order by level,code ,create_time desc limit ? , ?  "
								.toUpperCase(), (page - 1) * rows, rows);
	}

	@Override
	public List<SysFunction> queryByPage(long page, long rows, String parent)
			throws SQLException {
		if (parent == null || "".equals(parent))
			return super
					.query("select * from Sys_Function WHERE parent IS NULL  order by sort DESC,code ,create_time desc limit ? , ?  "
							.toUpperCase(), (page - 1) * rows, rows);
		else
			return super
					.query("select * from Sys_Function WHERE parent = ?  order by sort DESC,code ,create_time desc limit ? , ?  "
							.toUpperCase(), parent, (page - 1) * rows, rows);
	}

	public List<SysFunction> queryByPageType(long page, long rows, String type,
			String parent) throws SQLException {
		if (parent == null)
			return queryByPage(page, rows);
		else
			return super
					.query("select A.CODE,A.NAME,A.URL,A.AUTH,A.MODE,A.STATUS,A.CREATE_TIME,A.SORT,B.NAME AS PARENT  from Sys_Function A LEFT  JOIN SYS_FUNCTION B ON B.CODE = A.PARENT  WHERE A.type = ?  AND A.PARENT = ? order by  A.PARENT DESC,A.sort DESC,A.code ,A.create_time desc  limit ? , ?  "
							.toUpperCase(), type, parent, (page - 1) * rows,
							rows);
	}

	public List<SysFunction> queryByPageType(long page, long rows, String type)
			throws SQLException {
		return super
				.query("select A.CODE,A.NAME,A.URL,A.AUTH,A.MODE,A.STATUS,A.CREATE_TIME,A.SORT,B.NAME AS PARENT  from Sys_Function A LEFT  JOIN SYS_FUNCTION B ON B.CODE = A.PARENT  WHERE A.type = ? order by  A.PARENT DESC,A.sort DESC,A.code ,A.create_time desc  limit ? , ?  "
						.toUpperCase(), type, (page - 1) * rows, rows);
	}

	@Override
	public boolean update(SysFunction obj) throws SQLException {
		SqlBuilder sb = SqlBuilder.createUpdate(SysFunction.TABLE_NAME);
		sb.addField("name", obj.getName());
		if ("".equals(obj.getParent()))
			obj.setParent(null);
		sb.addField("parent", obj.getParent());
		sb.addField("url", obj.getUrl());
		sb.addField("auth", obj.getAuth());
		sb.addField("type", obj.getType());
		sb.addField("level", obj.getLevel());
		sb.addField("level_code", obj.getLevel_code());
		sb.addField("mode", obj.getMode());
		sb.addField("sort", obj.getSort());
		sb.addField("status", obj.getStatus());
		sb.addWhere("code", obj.getCode());
		return super.update(sb.getSql().toUpperCase(), sb.getParams());
	}
}
